<template>
  <div>
    <div class="header">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="所有订单" name="所有订单" />
        <el-tab-pane label="待支付" name="待支付" />
        <el-tab-pane label="待派单" name="待派单" />
        <el-tab-pane label="待接单" name="待接单" />
        <el-tab-pane label="待服务" name="待服务" />
        <el-tab-pane label="待确认" name="待确认" />
        <el-tab-pane label="已完成" name="已完成" />
      </el-tabs>
    </div>
    <div class="container">
      <el-table
        :data="tableData"
        stripe
        style="width: 100%"
        class="tableNumber"
      >
        <el-table-column prop="id" label="订单编号" />
        <el-table-column prop="orderTime" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.orderTime | fmtDate1 }}
          </template>
        </el-table-column>
        <el-table-column prop="total" label="总价">
          <template slot-scope="scope">
            <p class="total">
              ￥<span>{{ scope.row.total }}</span> 元
            </p>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === '待派单'" type="success"
              >待派单</el-tag
            >
            <el-tag v-else-if="scope.row.status === '待接单'" type="warning"
              >待接单</el-tag
            >
            <el-tag v-else type="info">待服务</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="customerId" label="顾客ID" />
        <el-table-column
          v-if="activeName !== '待派单' && activeName !== '待支付'"
          prop="employeeId"
          label="员工ID"
        />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <a href="#" class="details" @click.prevent="toDetails(scope.row.id)"
              >详情</a
            >
            <a
              v-if="scope.row.status === '待派单'"
              href="#"
              class="dispatch"
              @click.prevent="toSend(scope.row.id, '派单')"
              >派单</a
            >
            <a
              v-else-if="scope.row.status === '待接单'"
              href="#"
              class="accept"
              @click.prevent="toCancel(scope.row.id, '取消派单')"
              >取消派单</a
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态框 -->
    <div class="changeStatus">
      <el-dialog title="派单" :visible.sync="visible">
        <el-radio
          v-for="item in waiterData"
          :key="item.id"
          v-model="form.waiterId"
          :label="item.id"
          border
          size="medium"
          >{{ item.username }}{{ item.realname }}</el-radio
        >
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="send">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="page">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>
import {
  tableData,
  waiterData,
  sendOrder,
  cancelSendOrder,
} from "@/api/product";
import { fmtDate1 } from "@/filters/index";
export default {
  data() {
    return {
      activeName: "所有订单",
      tableData: [],
      params: {
        page: 1,
        pageSize: 10,
        roleId: 1,
      },
      total: 0,
      visible: false,
      form: {},
      waiterData: [],
    };
  },
  created() {
    this.getTableData();
    this.getWaiterData();
  },
  methods: {
    // 点击详情
    toDetails(id) {
      this.$router.push({
        path: "/admin/order/Details",
        query: {
          id,
        },
      });
    },
    // 取消订单
    async toCancel(id, status) {
      // console.log(id);
      const res = await cancelSendOrder({ orderId: id });
      this.$message({
        type: "success",
        message: res.message,
      });
      this.getTableData();
    },
    // 绑定确定按钮操作
    async send() {
      const res = await sendOrder(this.form);
      this.$message({
        type: "success",
        message: res.message,
      });
      delete this.form.waiterId;
      this.visible = false;
      this.getTableData();
    },
    // 获取form员工信息
    async getWaiterData() {
      const res = await waiterData(this.params);
      this.waiterData = res.data.list;
    },
    // 点击获取对应状态订单
    async handleClick() {
      if (this.activeName !== "所有订单") {
        this.params.status = this.activeName;
      } else {
        delete this.params.status;
      }
      this.getTableData();
    },
    // 获取表格数据
    async getTableData() {
      const res = await tableData(this.params);
      // console.log(res, "获取响应");
      this.tableData = res.data.list;
      this.total = res.data.total;
    },
    // 点击派单
    toSend(id, status) {
      this.visible = true;
      this.form.orderId = id;
    },
    // 点击每页几条
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.params.page = 1;
      this.getTableData();
    },
    // 点击第几页
    handleCurrentChange(page) {
      this.params.page = page;
      this.getTableData();
    },
  },
};
</script>
<style lang="scss">
a {
  margin-right: 15px;
}
.tableNumber {
  font-size: 13px;
}
.total span {
  color: red;
  font-size: 18px;
}
.dispatch {
  color: #30b08f;
}
.accept {
  color: #ffba00;
}
.dispatch:hover {
  color: #56cedd;
}
.accept:hover {
  color: #56cedd;
}
.details:hover {
  color: #56cedd;
}
.page {
  text-align: right;
}
</style>
      